from flask import Flask,render_template,abort
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import json,os

from pymongo import MongoClient
client = MongoClient('127.0.0.1',27017)
db_mongo = client.shiyanlou

app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD']=True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root@localhost/syltz8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class File(db.Model):
	id = db.Column(db.Integer,primary_key=True)
	title = db.Column(db.String(80))
	created_time = db.Column(db.DateTime)
	category_id = db.Column(db.Integer,db.ForeignKey('category.id'))
	content = db.Column(db.Text)
	category = db.relationship('Category',backref=db.backref('File', lazy='dynamic'))

	def __init__(self,title,created_time,category,content):
		self.title = title
		self.created_time =created_time
		self.category = category
		self.content = content

	def __repr__(self):
		return '<File %r>' % self.title
	def add_tag(self,tag_name):
		rec = db_mongo.tagrec.find_one({'id':self.id})
		if rec == None:
			new = {'id':self.id,'tags':[tag_name]}
			db_mongo.tagrec.insert_one(new)
		else:
			l = rec['tags']
			l.append(tag_name)
			db_mongo.tagrec.update_one({'id':self.id},{'$set':{'tags':l}})
	def remove_tag(self,tag_name):
		rec = db_mongo.tagrec.find_one({'id':self.id})
		if rec ==None:
			return
		else:
			list = rec['tags']
			list.remove(tag_name)
			db_mongo.tagrec.update_one({'id':self.id},{'$set':{'tags':list}})

	@property
	def tags(self):
		rec = db_mongo.tagrec.find_one({'id':self.id})
		if rec == None:
			return None
		else:
			return rec['tags']

class Category(db.Model):
	id = db.Column(db.Integer,primary_key=True)
	name = db.Column(db.String(80))

	def __init__(self,name):
		self.name = name
	def __repr__(self):
		return '<Category %r>' % self.name

@app.route('/')
def index():
		files_list = db.session.query(File).all()
		return render_template('index.html',files_list = files_list)

@app.route('/files/<file_id>')
def file(file_id):
		filename = db.session.query(File).filter(File.id==file_id).first_or_404()
		return render_template('file.html', i=filename)


@app.errorhandler(404)
def not_found(error):
		return render_template('404.html'),404

if __name__ =='__main__':

        app.run(port=3000)
